let pid = location.search.split('=')[1];
let obj = new URLSearchParams(location.search);
console.log(pid);
axios
  .get("http://jx.xuzhixiang.top/ap/api/detail.php", {
    params: { id: pid },
  })
  .then((res) => {
    let { data } = res;
    console.log(data);
    //商品详情数据
    let pObj = data.data;
    console.log(pObj.pid)
    let html = `
    <h1>${pObj.pname}</h1>

    <p>${pObj.pprice}</p>
    <img src="${pObj.pimg}" alt="" />

    <input type="number" name="" id=""  value='1' class="pnum-ipt"/>
   <input type="button" value="加入购物车"  class="cart-add" />

    <p>${pObj.pdesc}</p>
  `;
    let itemDiv = document.querySelector("#item-div");
    itemDiv.innerHTML = html; //  item div 才有内容  才有加入购物车按钮
    let cartAdd = document.querySelector(".cart-add");
    console.log(cartAdd); //null
    cartAdd.onclick = async function () {
      console.log(111);
      //uid  用户id
      // pid  商品id
      // pnum  要添加的商品数量
      let uid = localStorage.getItem("uid");
      let pnumIpt = document.querySelector(".pnum-ipt");
      let pnum = pnumIpt.value;
      let params = { uid, pid, pnum };
      console.log(params);
      let { data } = await axios.get("http://jx.xuzhixiang.top/ap/api/add-product.php", { params });
      console.log(data);
    };
  });

// 必须等页面有 了按钮 ，才能去获取
let cartAdd = document.querySelector(".cart-add");
console.log(cartAdd); //nul